##########################################################################################

library(data.table)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--sample"), type = "character"),
    make_option(c("--ccf_file"), type = "character"),
    make_option(c("--out_file"), type = "character")
)

if(1!=1){
    
    sample <- "WGC079294"
    ccf_file <- "/public/user/xxf/20210520_LCSomatic/titan/chat/WGC079294_CHAT.txt"
    out_file <- "/public/user/xxf/20210520_LCSomatic/mutationTime/input/WGC079294.vcf"
}

##########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sample <- opt$sample
ccf_file <- opt$ccf_file
out_file <- opt$out_file

##########################################################################################

dat <- data.frame(fread(ccf_file))

## MutationTime input
## vcf
## 头文件还需要CAT补
## #CHROM  POS     ID      REF     ALT     QUAL    FILTER  INFO    FORMAT  Tumor
t_alt_count <- round(dat$Depth * dat$VAF)
t_ref_count <- dat$Depth - t_alt_count
INFO <- paste("t_ref_count=",t_ref_count,";","t_alt_count=",t_alt_count,sep="")

vcf_out <- data.frame(CHROM = dat[,"Chr"] , POS = dat[,"Start_Position"] , ID = "." , REF = dat[,'REF'] , ALT = dat[,'ALT'] , 
	QUAL = "." , FILTER = "." , INFO = INFO , FORMAT = "." , Tumor = "." )
vcf_out <- vcf_out[which(t_alt_count>0),]
colnames(vcf_out)[1] <- "#CHROM"

write.table(vcf_out , out_file , col.names = T , row.names = F , sep = "\t" , quote = F)
